/**
 * 
 */
package cn.ac.istic.lkt.atv;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Chongde Shi
 * 
 */



public class TreePanel2Tree extends TreePanelAbstract {

	private static final long serialVersionUID = 6648847195991676003L;

	private ParseTree pt1 = null;
	private ParseTree pt2 = null;
	private ArrayList<Align> align = null;
	private boolean directionfe = true;
	
	public class Align{
		public int f;
		public int e;
		public Align(int x,int y){
			e=x;
			f=y;
		}
	}
	public TreePanel2Tree(){
		super();

	}

	public void init(ParseTree pt1, ParseTree pt2, String a, String d){
		this.pt1 = pt1;
		this.pt2 = pt2;		
		this.setDirection(d);
		buildAlign(a);
	}

	public void buildAlign(String s) {
		try {
			String[] a = s.trim().split(" ");
			align = new ArrayList<Align>(a.length);
			for (int i = 0; i < a.length; i++) {
				String[] b = a[i].split("-");
				int e = Integer.parseInt(b[0]);
				int f = Integer.parseInt(b[1]);
				//System.err.println(""+e+" "+f+" "+this.pt1.getWordCount()+" "+this.pt2.getWordCount());
				if((directionfe && (e>=this.pt2.getWordCount() || f>=this.pt1.getWordCount())) 
					|| (!directionfe && (e>=this.pt1.getWordCount() || f>=this.pt2.getWordCount()))){
					System.err.println("Alignment out of range, discard it.");
					this.align = null;
					return;
				}
				align.add(new Align(e,f));
			}
			
		} catch (Exception e) {
			System.err.println("Parsing alignment error: " + s);
			align = null;
		}
	}

	public void myPaint(Graphics2D g2) {
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setFont(TFONT);
		FontRenderContext frc = g2.getFontRenderContext();
		charHeight = (float) TFONT.getMaxCharBounds(frc).getHeight();
		charWidth = ((float) TFONT.getMaxCharBounds(frc).getWidth());

		if (this.pt1 == null || this.pt2 == null) {
			g2.drawString("trees", 10, 20);
			return;
		}

		calcPos(frc);
		List<PTNode> nodelist1 = pt1.getNodeList();
		for (int i = 0; i < nodelist1.size(); i++) {
			PTNode nd = nodelist1.get(i);
			if (nd.isWord()) {
				g2.setFont(TFONT);
			} else
				g2.setFont(TFONT);
			g2.drawString(nd.getLabel(), nd.getX(), nd.getY());
		}
		List<PTNode> nodelist2 = pt2.getNodeList();
		for (int i = 0; i < nodelist2.size(); i++) {
			PTNode nd = nodelist2.get(i);
			if (nd.isWord()) {
				g2.setFont(TFONT);
			} else
				g2.setFont(TFONT);
			g2.drawString(nd.getLabel(), nd.getX(), nd.getY());
		}

		drawLines(g2, pt1, nodelist1.get(0));
		drawLines(g2, pt2, nodelist2.get(0));
		if(this.align != null) drawAlign(g2);
	}

	public void drawAlign(Graphics2D g2){
		List<PTNode> ndf = pt1.getNodeList();
		List<PTNode> nde = pt2.getNodeList();
		for (int i=0; i< align.size(); i++){
			Align a = align.get(i);
			PTNode tnf,tne;
			if(this.directionfe){
				//System.err.println("fe: "+a.f+" "+a.e);
				tnf = ndf.get(pt1.getWordPos().get(a.f));
				tne = nde.get(pt2.getWordPos().get(a.e));
			}
			else{
				//System.err.println("ef: "+a.f+" "+a.e);
				tnf = ndf.get(pt1.getWordPos().get(a.e));
				tne = nde.get(pt2.getWordPos().get(a.f));
			}
			Shape line = new java.awt.geom.Line2D.Double( tnf.getXmiddle(),
					tnf.getYdown(), tne.getXmiddle(), tne.getYdown());
			g2.draw(line);
		}
		
	}
	public void calcPos(FontRenderContext frc) {
		pt1.setX(frc);// 设置每一个树节点的X坐标
		pt2.setX(frc);// 设置每一个树节点的X坐标
		// 上两个方法相同
		pt1.setY(frc); // 设置每一个树节点的Y坐标
		pt2.setY(frc, pt1.getMaxY()); // 设置每一个树节点的Y坐标
		// 这两个方法不同，pt2树朝下
		if (pt1.getMaxX() > pt2.getMaxX()) {
			imgWidth = (int) (pt1.getMaxX() + STARTX);
		} else
			imgWidth = (int) (pt2.getMaxX() + STARTX);
		if (pt1.getMaxY() > pt2.getMaxY()) {
			imgHeight = (int) (pt1.getMaxY() + STARTX);
		} else
			imgHeight = (int) (pt2.getMaxY() + STARTX);
	}
	
	public void setDirection(String d){
		if (d.equals("fe")){
			this.directionfe = true;
		}
		else if (d.equals("ef")){
			this.directionfe = false;
		}
		else{
			System.err.println("Wrong alignment direction, using default: f-e");
		}
	}

}
